import Vue from 'vue'
import Router from 'vue-router'
import NProgress from 'nprogress'
import Index from '@/view/Index/Index'
import ExcellentCourse from '@/view/ExcellentCourse/Index'
import LiveBroadcastCourse from '@/view/LiveBroadcastCourse/Index'
import PublicCourse from '@/view/PublicCourse/Index'
import WeChatCourse from '@/view/WeChatCourse/Index'
import PersonalCenter from '@/view/PersonalCenter/Index'
import InQuire from '@/view/PersonalCenter/InQuire'
import Collection from '@/view/PersonalCenter/Collection'
import History from '@/view/PersonalCenter/History'
import PasswordBack from '@/view/PersonalCenter/PasswordBack'
import NewPassword from '@/view/PersonalCenter/NewPassword'
import CourseManagement from '@/view/PersonalCenter/CourseManagement'
import CourseManagementList from '@/view/PersonalCenter/CourseManagementList'
import LiveManagement from '@/view/PersonalCenter/LiveManagement'
import Teacher from '@/view/PersonalCenter/Teacher'
import LiveMore from '@/view/LiveBroadcastCourse/LiveMore'
import SearchPage from '@/view/Index/SearchPage'
import VideoPage from '@/view/VideoPage/Index'
import LiveAll from '@/view/LiveBroadcastCourse/LiveAll'
import Message from '../components/Layout/message'

Vue.use(Router)

const router = new Router({
  mode: 'history',
  routes: [
    {
      path: '/',
      name: 'Index',
      component: Index,
      meta: {
        title: '首页'
      }
    },
    {
      path: '/publiccourse',
      name: 'PublicCourse',
      component: PublicCourse,
      meta: {
        title: '公开课'
      }
    },
    {
      path: '/excellentcourse',
      name: 'ExcellentCourse',
      component: ExcellentCourse,
      meta: {
        title: '精品课'
      }
    },
    {
      path: '/wechatcourse',
      name: 'WeChatCourse',
      component: WeChatCourse,
      meta: {
        title: '微课'
      }
    },
    {
      path: '/livebroadcastcourse/:name?',
      name: 'LiveBroadcastCourse',
      component: LiveBroadcastCourse,
      meta: {
        title: '直播课'
      }
    },
    {
      path: '/livedetail',
      name: 'LiveMore',
      component: LiveMore,
      meta: {
        title: '直播详情'
      }
    },
    {
      path: '/personalcenter',
      name: 'PersonalCenter',
      component: PersonalCenter,
      meta: {
        title: '个人中心'
      }
    },
    {
      path: '/inquire',
      name: 'InQuire',
      component: InQuire,
      meta: {
        title: '学分查询'
      }
    },
    {
      path: '/collection',
      name: 'Collection',
      component: Collection,
      meta: {
        title: '我的收藏'
      }
    },
    {
      path: '/history',
      name: 'History',
      component: History,
      meta: {
        title: '历史记录'
      }
    },
    {
      path: '/passwordback',
      name: 'PasswordBack',
      component: PasswordBack,
      meta: {
        title: '密码找回'
      }
    },
    {
      path: '/newpassword',
      name: 'NewPassword',
      component: NewPassword,
      meta: {
        title: '修改密码'
      }
    },
    {
      path: '/coursemanagement',
      name: 'CourseManagement',
      component: CourseManagement,
      meta: {
        title: '课程管理'
      }
    },
    {
      path: '/coursemanagementlist',
      name: 'CourseManagementList',
      component: CourseManagementList,
      meta: {
        title: '课程管理列表'
      }
    },
    {
      path: '/livemanagement',
      name: 'LiveManagement',
      component: LiveManagement,
      meta: {
        title: '直播管理'
      }
    },
    {
      path: '/searchpage/:name?',
      name: 'SearchPage',
      component: SearchPage,
      meta: {
        title: '搜索页面'
      }
    },
    {
      path: '/videopage/:id',
      name: 'VideoPage',
      component: VideoPage,
      meta: {
        title: '视频播放'
      }
    },
    {
      path: '/liveall/:name?',
      name: 'LiveAll',
      component: LiveAll,
      meta: {
        title: '直播分类'
      }
    },
    {
      path: '/teacher',
      name: 'Teacher',
      component: Teacher,
      meta: {
        title: '教师简介'
      }
    }
  ]
})

NProgress.configure({showSpinner: false})

router.beforeEach((to, from, next) => {
  console.log('router is to ', to)
  NProgress.start()
  if (to.meta.title) {
    document.title = to.meta.title
  }

  const urls = ['personalcenter', 'livemanagement', 'inquire', 'history', 'collection', 'coursemanagement', 'coursemanagementlist', 'livebroadcastcourse']
  // const urls = ['personalcenter', 'livemanagement', 'inquire', 'history', 'collection',
  //   'coursemanagement', 'coursemanagementlist', 'passwordback']

  let idx = urls.indexOf(to.name.toLocaleLowerCase())
  console.log('router is to idx', idx)
  if (idx > -1) {
    let isSignIn = JSON.parse(window.sessionStorage.getItem('IS-SIGN-IN'))
    console.log('router is to isSignIn', isSignIn)
    if (isSignIn) {
      next()
    } else {
      next({path: '/'})
      Message.error('请登录!')
    }
  } else {
    next()
  }
})

router.afterEach(transition => {
  NProgress.done()
})

export default router
